package com.dinglexiao.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dinglexiao.common.lang.Result;
import com.dinglexiao.entity.Blog;
import com.dinglexiao.service.BlogService;
import com.dinglexiao.utils.JwtUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dlx
 * @since 2020-09-16
 */
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    BlogService blogService;

    @Autowired
    JwtUtils jwtUtils;

    /***
    * @description:分页查询博客列表
    * @param
    * @return com.dinglexiao.common.lang.Result
    * @author dlx
    * @date 2020/9/18-14:34
    */
    @GetMapping("/list")
    public Result blogList(@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
                           @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        Page page = new Page(currentPage,pageSize);
        IPage pageData = blogService.page(page, new QueryWrapper<Blog>().orderByDesc("created"));
        return Result.success(pageData);
    }

    /***
    * @description:博客详细页面
    * @param id
    * @return com.dinglexiao.common.lang.Result
    * @author dlx
    * @date 2020/9/18-14:52
    */
    @GetMapping("/detail/{id}")
    public Result blogDetail(@PathVariable("id") Integer id){
        Blog blog = blogService.getById(id);
        Assert.notNull(blog,"该博客内容已被删除！");
        Result result = Result.success(blog);
        return result;
    }

    /***
    * @description:修改博客
    * @param newBlog
    * @return com.dinglexiao.common.lang.Result
    * @author dlx
    * @date 2020/9/18-14:54
    */
    @RequiresAuthentication
    @PostMapping("/edit")
    public Result blogEdit(@Validated @RequestBody Blog newBlog, HttpServletRequest request){
        //如果存在id，表明是修改博客
        String token = request.getHeader("token");
        Integer userID = Integer.parseInt(jwtUtils.getClaimByToken(token).getSubject());
        Blog blog = null;
        if(newBlog.getId() != null){
            blog = blogService.getById(newBlog.getId());
            //1.判断是否是当前登录用户所属的博客
            Assert.isTrue(blog.getUserId().equals(userID),"缺少编辑权限！");

        }else {
            blog = new Blog();
            blog.setUserId(userID);
            blog.setCreated(LocalDateTime.now());
            blog.setStatus(1);
        }
        BeanUtil.copyProperties(newBlog,blog,"id","userId","status");
        try {
            blogService.saveOrUpdate(blog);
            return Result.success("修改成功！");
        }catch (Exception e){
            return Result.fail("修改失败！");
        }
    }

    /***
    * @description:删除博客
    * @param id
    * @return com.dinglexiao.common.lang.Result
    * @author dlx
    * @date 2020/9/24-20:45
    */
    @RequestMapping("/delBlog")
    public Result deleteBlog(@RequestParam(value = "blogId") Integer id){
        try {
            blogService.removeById(id);
            return Result.success("删除成功！");
        }catch (Exception e){
            return Result.fail("删除失败！");
        }
    }
}
